package ai.dao;

import ai.bussness.DBOperation;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by 杨济瑞 on 2016/8/10.
 */
public class Select {
    Connection conn;
    PreparedStatement ps;
    DBOperation dbOperation = new DBOperation();
    ResultSet rs;

    /**
     * 按姓名查询记录是否存在，存在就返回记录号
     *
     * @param name 姓名
     * @return 记录号
     */
    public int selectMember(String name) {
        String sql = "select MEMBER_ID from MEMBER where MEMBER_NAME = ?";
        conn = dbOperation.getConnection();
        String ret = null;
        int id = -1;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            rs = ps.executeQuery();
            String resname;
            if (rs.next()) {
                //获取stuname这列数据
//                resname = rs.getString("MEMBER_NAME");
                //获取stuid这列数据
                id = rs.getInt("MEMBER_ID");
                ret = "" + id;
                System.out.println(ret);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return id;
    }
}
